package controller;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import layout.Mensaje;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.example.test.R.layout;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.util.Log;

public class Controlador {
	// private static final String WSDL =
	// "http://192.168.1.5/EducareWebService/servicioSOA.php?wsdl";
	private static final String NAMESPACE = "http://192.168.1.5/EducareWebService/servicioSOA.php";
	
	public static String obtenerUsuarioPorToken(String token_publicacion) {

		String METHOD_NAME = "obtenerUsuarioPorToken";
		String SOAP_ACTION = NAMESPACE + "#" + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		Log.i("SOAP_ACTION", SOAP_ACTION);

		request.addProperty("token_publicacion", token_publicacion);


		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE transport = new HttpTransportSE(NAMESPACE);

		try {
			transport.call(SOAP_ACTION, envelope);
			Log.i("RESPONSE", envelope.getResponse().toString());
			return envelope.getResponse().toString();

		} catch (Exception e) {
			e.printStackTrace();
			return e.toString();
		}

	}
	
	public static String registro(String user, String password, String nombres, String apellidos) {

		String METHOD_NAME = "registraUsuario";
		String SOAP_ACTION = NAMESPACE + "#" + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		Log.i("SOAP_ACTION", SOAP_ACTION);

		request.addProperty("username", user);
		request.addProperty("password", password);
		request.addProperty("nombres", nombres);
		request.addProperty("apellidos", apellidos);


		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE transport = new HttpTransportSE(NAMESPACE);

		try {
			transport.call(SOAP_ACTION, envelope);
			Log.i("RESPONSE", envelope.getResponse().toString());
			return envelope.getResponse().toString();

		} catch (Exception e) {
			e.printStackTrace();
			return e.toString();
		}

	}
	
	public static String login(String user, String password) {

		String METHOD_NAME = "login";
		String SOAP_ACTION = NAMESPACE + "#" + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		Log.i("SOAP_ACTION", SOAP_ACTION);

		request.addProperty("username", user);
		request.addProperty("password", password);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE transport = new HttpTransportSE(NAMESPACE);

		try {
			transport.call(SOAP_ACTION, envelope);
			Log.i("RESPONSE", envelope.getResponse().toString());
			return envelope.getResponse().toString();

		} catch (Exception e) {
			e.printStackTrace();
			return e.toString();
		}

	}

	public static void publicar(String username, String titulo, String texto) {

		String METHOD_NAME = "publicar";
		String SOAP_ACTION = NAMESPACE + "#" + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		Log.i("SOAP_ACTION", SOAP_ACTION);

		request.addProperty("username", username);
		request.addProperty("titulo", titulo);
		request.addProperty("texto", texto);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE transport = new HttpTransportSE(NAMESPACE);

		try {
			transport.call(SOAP_ACTION, envelope);
			Log.i("RESPONSE", envelope.getResponse().toString());
			return;

		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

	}
	
	public static void enviarMensaje(String username_emisor, String username_receptor, String texto) {

		String METHOD_NAME = "enviarMensaje";
		String SOAP_ACTION = NAMESPACE + "#" + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		Log.i("SOAP_ACTION", SOAP_ACTION);

		request.addProperty("texto", texto);
		request.addProperty("username_emisor", username_emisor);
		request.addProperty("username_receptor", username_receptor);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE transport = new HttpTransportSE(NAMESPACE);

		try {
			transport.call(SOAP_ACTION, envelope);
			Log.i("RESPONSE", envelope.getResponse().toString());
			return;

		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

	}

	public static ArrayList<domain.PublicacionAndroidClass> getPublicaciones() {

		String METHOD_NAME = "obtenerPublicaciones";
		String SOAP_ACTION = NAMESPACE + "#" + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		
		
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE transport = new HttpTransportSE(NAMESPACE);

		try {
			transport.call(SOAP_ACTION, envelope);

			Type listType = new TypeToken<ArrayList<domain.PublicacionAndroidClass>>() {}.getType();
			ArrayList<domain.PublicacionAndroidClass> publicaciones = new Gson()
					.fromJson(envelope.getResponse().toString(), listType);

			return publicaciones;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}
	
	public static ArrayList<domain.ComentarioClass> getComentarios(String token_publicacion) {

		String METHOD_NAME = "obtenerComentariosPublicacion";
		String SOAP_ACTION = NAMESPACE + "#" + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		
		Log.i("TOKEN_PUBLICACION", token_publicacion);
		
		request.addProperty("token_publicacion",token_publicacion);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE transport = new HttpTransportSE(NAMESPACE);

		try {
			transport.call(SOAP_ACTION, envelope);

			Type listType = new TypeToken<ArrayList<domain.ComentarioClass>>() {}.getType();
			ArrayList<domain.ComentarioClass> comentarios = new Gson()
					.fromJson(envelope.getResponse().toString(), listType);

			return comentarios;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}
	
	public static void comentarPublicacion(String texto, String token_publicacion, String username) {

		String METHOD_NAME = "comentarPublicacion";
		String SOAP_ACTION = NAMESPACE + "#" + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		Log.i("SOAP_ACTION", SOAP_ACTION);

		request.addProperty("texto", texto);
		request.addProperty("token_publicacion", token_publicacion);
		request.addProperty("username", username);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE transport = new HttpTransportSE(NAMESPACE);

		try {
			transport.call(SOAP_ACTION, envelope);
			Log.i("RESPONSE", envelope.getResponse().toString());
			return;

		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

	}
	
	public static ArrayList<Mensaje> getMensajes(String username) {

		String METHOD_NAME = "obtenerMensajes";
		String SOAP_ACTION = NAMESPACE + "#" + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		

		
		request.addProperty("username",username);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE transport = new HttpTransportSE(NAMESPACE);

		try {
			transport.call(SOAP_ACTION, envelope);

			Type listType = new TypeToken<ArrayList<Mensaje>>() {}.getType();
			ArrayList<Mensaje> mensajes = new Gson()
					.fromJson(envelope.getResponse().toString(), listType);

			return mensajes;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

}
